package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.JoinListBean;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class JoinListVM extends TitleVM {
    public ObservableField<String> companyid = new ObservableField<>();

    public JoinListVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("申请信息");

    }


    @Override
    public void onResume() {
        super.onResume();
        getJoinData();
    }

    public ObservableList<JoinListItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<JoinListItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_join_list);

    public ObservableField<Integer> showNo = new ObservableField<>(View.VISIBLE);
    private void getJoinData() {
        Map<String,String> map = new HashMap<>();
        map.put("companyid",companyid.get());
        model.joincompanylist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<JoinListBean>() {
                    @Override
                    public void onNext(JoinListBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size() > 0){
                                showNo.set(View.GONE);
                                for (JoinListBean.DataDTO bean : o.getData()) {
                                    JoinListItemVM itemVM = new JoinListItemVM(JoinListVM.this);
                                    itemVM.setData(bean);
                                    observableList.add(itemVM);

                                }
                            }else {
                                showNo.set(View.VISIBLE);
                            }


                        }else {
                            showNo.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNo.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void handle(String sid,String state){
        Map<String,String> map = new HashMap<>();
        map.put("sid",sid);
        map.put("state",state);
        model.handle(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult o) {
                        if (o.issucc){
                            onResume();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

}
